/*
 * Copyright Notice ====================================================
 * This file contains proprietary information of JeeStrap Co.
 * Copying or reproduction without prior written approval is prohibited.
 * Copyright (c) 2014 All rights reserved. =============================
 */

package com.jeestrap.common.smartcoder.generator;

import java.io.File;
import java.util.Map;

import com.jeestrap.common.smartcoder.domain.CodeDesc;
import com.jeestrap.common.smartcoder.util.FreeMarkers;

import freemarker.template.Configuration;
import freemarker.template.Template;

public abstract class CodeGenerator {

	protected Template template;

	protected Map<String, Object> model;

	protected String separator = File.separator;

	protected String javaPath;

	protected String viewPath;
	
	public CodeGenerator(Map<String, Object> model){
		this.model = model;
	}

	public String generate(CodeDesc codeDesc) throws Exception{
		String content = "";
		try {
			Configuration cfg = new Configuration();
			// 代码模板配置
			String tplPath = ClassLoader.getSystemResource("template/").getFile();
			System.out.println("Template Path: {}" +  tplPath);
			
			cfg.setDirectoryForTemplateLoading(new File(tplPath));
			content = FreeMarkers.renderTemplate(cfg.getTemplate(getTemplate()), getVariableContext(codeDesc));
		} catch (Exception e) {
			System.out.println(e.getMessage());
			throw e;
		}
		return content;
	}

	public abstract String getTemplate();
	
	public abstract Map<String, Object> getVariableContext(CodeDesc codeDesc);

}
